package cn.tedu.controller;

import cn.tedu.pojo.Item;
import cn.tedu.service.ItemService;
import cn.tedu.util.RedisConstant;
import cn.tedu.vo.JsonResult;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kiven
 */
@RestController
@RequestMapping("/api/v1/cart")
@Api(tags = {"用户购物车接口"})
public class CartController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ItemService itemService;

    @PostMapping("/addCart")
    public JsonResult addCart(int itemId,int buyNum){
        //获取购物车
        BoundHashOperations<String, String, Object> myCartOps = getMyCartOps();
        Object o = myCartOps.get(itemId + "");
        String result = "";
        if(o!=null){
            result = (String)o;
        }
        //购物车没有这个商品
        if(o == null){
            //直接数据库查一个放进去
            Item itemById = itemService.getById(itemId+"");
            itemById.setNum(buyNum);
            String s = JSON.toJSONString(itemById);
            myCartOps.put(itemId+"",s);
        }else{
            //数量增加,再放进去
            Item i = JSON.parseObject(result,Item.class);
            i.setNum( i.getNum()+ buyNum);
            String s = JSON.toJSONString(i);
            myCartOps.put(itemId+"",s);
        }
        return JsonResult.success("添加成功");
    }

    /**
     * 查看redis购物车内容
     * @return
     */
    @GetMapping("/findCartItems")
    public JsonResult getMyCartItems(){
        // 根据getMyCartOps里面的uid得到
        BoundHashOperations<String, String, Object> myCartOps = getMyCartOps();
        List<Object> values = myCartOps.values();
        List<Item> cartItems = new ArrayList<>();
        assert values != null;
        for(Object o : values){
            String s = JSON.toJSONString(o);
            Item i = JSON.parseObject(s,Item.class);
            cartItems.add(i);
        }

        return JsonResult.success("获取购物车信息成功",cartItems);

    }

    @GetMapping("/deleteCartItem/{id}")
    public JsonResult deleteCartItem(@PathVariable("id") String id){
        BoundHashOperations<String, String, Object> myCartOps = getMyCartOps();
        myCartOps.delete(id);
        return JsonResult.success("删除成功");
    }

    /**
     * 清空我得购物车
     * @return
     */
    @RequestMapping("/clear")
    public JsonResult clear(){
        String cartKey = getCartKey();
        redisTemplate.delete(cartKey);
        return JsonResult.success("清空购物车成功");
    }

    private BoundHashOperations<String,String,Object> getMyCartOps(){
        String cartKey = getCartKey();
        System.out.println(cartKey);
        return redisTemplate.boundHashOps(cartKey);
    }

    private String getCartKey(){
        // 从session或者jwt等等里面获取userId
        int userId = 88;
        return String.format(RedisConstant.USER_CART+"%s",userId);
    }

}
